﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace SimpleTFTP
{
    /*
     ERROR CODES:
     * 00 - Not defined, see error message (if any).
     * 01 - File not found.
     * 02 - Access violation.
     * 03 - Disk full or allocation exceeded.
     * 04 - Illegal TFTP operation.
     * 05 - Unknown transfer ID.
     * 06 - File already exists.
     * 07 - No such user.
     */

    internal class ErrorPacket : BasePacket
    {
        public short ErrorCode { get; set; }

        public string ErrorText { get; set; }

        public ErrorPacket(short errorCode, string errorText)
        {
            this.OpCode = OpCode.ERROR;
            this.ErrorCode = errorCode;
            this.ErrorText = errorText;
        }

        public override byte[] ToByteArray()
        {
            List<byte> result = new List<byte>();

            result.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)this.OpCode)));
            result.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(this.ErrorCode)));
            result.AddRange(Encoding.ASCII.GetBytes(this.ErrorText));

            return result.ToArray();
        }
    }
}